package com.example.myapplication.chap02;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class FirstActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText editText = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_02_first);
        Button bt = findViewById(R.id.button2);
        editText = findViewById(R.id.ed_first);
        bt.setOnClickListener(this);
        Button bt3 = findViewById(R.id.button3);
        bt3.setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
    }

    private ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == RESULT_OK) {
                        Intent in  = result.getData();
                        // 处理结果
                        String str = in.getStringExtra("result_str");
                        editText.setText(str);

                    }
                }
            }
    );
    public void startSecondActivity() {
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("ed",editText.getText().toString());
        someActivityResultLauncher.launch(intent);
    }
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.button2){
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 	// 创建Intent对象
            intent.putExtra("ed",editText.getText().toString());
            startActivity(intent);
            //startActivityForResult(intent,1000);
        }else if(view.getId() == R.id.button3){
                startSecondActivity();
        }else if(view.getId() == R.id.button4){
            requestPermission();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.v("onActivityResult", resultCode +" + "+requestCode);
        if(requestCode == 1000){
            if(resultCode == RESULT_OK){
                String str = data.getStringExtra("result_str");
                editText.setText(str);
            }
        }
    }

    private ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(
            new ActivityResultContracts.RequestPermission(),
            new ActivityResultCallback<Boolean>() {
                @Override
                public void onActivityResult(Boolean granted) {
                    if (granted) {
                        // 权限被授予
                    }
                }
            }
    );

    public void requestPermission() {
        requestPermissionLauncher.launch(Manifest.permission.CAMERA);
    }
}
